#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

typedef struct sockaddr* SP;

//139.224.117.106公

int main()
{
	printf("1、创建socket...\n");
	int sockfd = socket(AF_INET,SOCK_DGRAM,0);
	if(0 > sockfd)
	{
		perror("socket");
		return -1;
	}

	printf("2、准备地址...\n");
	struct sockaddr_in addr = {};
	addr.sin_family = AF_INET;
	addr.sin_port = htons(8866);
	addr.sin_addr.s_addr = inet_addr("139.224.117.106");
	
	socklen_t len = sizeof(addr);
	char buf[1024] = {};

	for(;;)
	{
		printf(">");
		gets(buf);
		sendto(sockfd,buf,strlen(buf)+1,0,(SP)&addr,len);
		if(0 == strcmp("quit",buf))
		{
			printf("通信结束!\n");
			break;
		}
		recvfrom(sockfd,buf,sizeof(buf),0,(SP)&addr,&len);
		printf("recv:%s\n",buf);
	}
	//关闭socket
	close(sockfd);
}
